package org.foreveross.jbpm.core.service.impl;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.drools.persistence.info.WorkItemInfo;
import org.drools.runtime.StatefulKnowledgeSession;
import org.foreveross.jbpm.core.KoalaProcessInfo;
import org.foreveross.jbpm.core.service.JBPMTaskService;
import org.jbpm.bpmn2.xml.di.BPMNPlaneHandler.ProcessInfo;
import org.jbpm.persistence.processinstance.ProcessInstanceInfo;
import org.jbpm.task.Content;
import org.jbpm.task.OrganizationalEntity;
import org.jbpm.task.Status;
import org.jbpm.task.Task;
import org.jbpm.task.TaskService;
import org.jbpm.task.User;
import org.jbpm.task.query.TaskSummary;

@Named("jbpmTaskService")
public class JBPMTaskServiceImpl implements JBPMTaskService {

	@Inject
	@PersistenceContext(unitName="jbpm")
	EntityManager entityManager;
	
	public void removeTaskUser(long taskId,String user){
	    entityManager.createNativeQuery("delete from peopleassignments_potowners where TASK_ID='"+taskId+"' and ENTITY_ID='"+user+"' ").executeUpdate();
	    
	}
	
	public void failedTask(Task task) {
		task.getTaskData().setStatus(Status.Failed);
		entityManager.merge(task);
	}
	
	public void repairTask(Task task){
		task.getTaskData().setStatus(Status.Reserved);
		entityManager.merge(task);
	}

	public List<TaskSummary> getErrorTasks() {
        final Query tasksAssignedAsPotentialOwner = entityManager.createNamedQuery("GetErrorTasks");
        tasksAssignedAsPotentialOwner.setParameter("language", "en-UK");
        return (List<TaskSummary>) tasksAssignedAsPotentialOwner.getResultList();
	}
	
	public List<WorkItemInfo> getWorkItemInfo(long processInstanceId){
		final Query workItemQuery = entityManager.createQuery("select w from WorkItemInfo w where w.processInstanceId = :processInstanceId");
		workItemQuery.setParameter("processInstanceId", processInstanceId);
		return (List<WorkItemInfo>)workItemQuery.getResultList();
	}
	
	public void exitedTask(long processInstanceId){
		final Query workItemQuery = entityManager.createQuery("select w from Task w where w.taskData.processInstanceId = :processInstanceId");
		workItemQuery.setParameter("processInstanceId", processInstanceId);
		List<Task> tasks = (List<Task>)workItemQuery.getResultList();
		for(Task task:tasks){
			if(task.getTaskData().getStatus().equals(Status.Failed) || task.getTaskData().getStatus().equals(Status.InProgress) || task.getTaskData().getStatus().equals(Status.Reserved)){
				task.getTaskData().setStatus(Status.Exited);
				entityManager.merge(task);
			}
		}
	}
	
	public void addProcessInfo(KoalaProcessInfo info){
		entityManager.persist(info);
		
	}
	
	public void removeAssignUser(User user){
		entityManager.remove(entityManager.merge(user));
	}
	public void removeWorkItemInfo(WorkItemInfo info){
		entityManager.remove(info);
	}
	
	public void saveWorkItem(WorkItemInfo info){
		entityManager.persist(info);
	}
	
	public ProcessInstanceInfo getProcessInstanceInfo(long processInstanceId){
		return entityManager.find(ProcessInstanceInfo.class, processInstanceId);
	}

	public void updateProcessInstanceInfo(ProcessInstanceInfo info){
		entityManager.merge(info);
	}
	
	public Content getContent(long contentId){
		return entityManager.find(Content.class, contentId);
	}
}
